package kr.saeildamil.androidstudy_second.actionbar;

import kr.saeildamil.androidstudy_second.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class ActionTab extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actiontab);

		ActionBar ab = getActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		for (int i = 0; i < 3; i++) {
			ActionBar.Tab tab = ab.newTab();
			String Cap = "Tab" + (i + 1);
			tab.setText(Cap);
			TabFragment frag = TabFragment.newInstance(Cap);
			tab.setTabListener(new TabListener(frag));
			ab.addTab(tab);
		}

		if (savedInstanceState != null) {
			int seltab = savedInstanceState.getInt("seltab");
			ab.setSelectedNavigationItem(seltab);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("seltab", getActionBar().getSelectedNavigationIndex());
	}

	private class TabListener implements ActionBar.TabListener {
		private Fragment mFragment;

		public TabListener(Fragment mFragment) {
			super();
			this.mFragment = mFragment;
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.ch21_actiontabarent, mFragment, "tag");
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(mFragment);
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {

		}

	}

}
